﻿@using System.Globalization
@using Lari.Configuration
@using Lari.Framework.Utility
@using Lari.Web.Utility
@model Lari.Services.ViewModel.UserSearchModel
@{
    string s = Model == null ? string.Empty : Model.SearchString;

    if (string.IsNullOrEmpty(s))
    {
        var cookie = HttpContext.Current.Request.Cookies[CookieName.LastEmployeeUserSerch];
        if (cookie != null && cookie.Value != null)
        {
            s = Encryption.Decrypt(cookie.Value);
            
        }
    }
    
}
@helper BuildLinks(int start, int end, string innerContent)
{
        for (int i = start; i <= end; i++)
        {
            if (i == Model.CurrentPage)
            {
            <text>
            @i.ToString(CultureInfo.InvariantCulture)
            </text> 
            }
            else
            {
            <a href="@Url.Action(ActionConst.Users.Search, ControllerConst.Users, new { search = Model.SearchString, currentPage = i })">@(innerContent ?? i.ToString(CultureInfo.InvariantCulture))</a> 
            }

        }
     
}
@helper BuildPaginations()
    {
        if (Model.TotalPageCount <= ApplicationConfig.ShowTotalPage)
        {
        @BuildLinks(1, Model.TotalPageCount, null)
            return;
        }
        int pagesAfter = Model.TotalPageCount - Model.CurrentPage; // Number of pages after current
        int pagesBefore = Model.CurrentPage - 1; // Number of pages before current

        if (pagesAfter <= 4)
        {
        @BuildLinks(1, 1, null) // Show 1st page

            int pageSubset = Model.TotalPageCount - ApplicationConfig.ShowTotalPage - 1 > 1 ? Model.TotalPageCount - ApplicationConfig.ShowTotalPage - 1 : 2;
        @BuildLinks(pageSubset, pageSubset, "...") // Show page subset (...)
 
        @BuildLinks(Model.TotalPageCount - ApplicationConfig.ShowTotalPage + 3, Model.TotalPageCount, null) // Show last pages

            return; // Exit
        }

        if (pagesBefore <= 4)
        {
           @BuildLinks(1, ApplicationConfig.ShowTotalPage - 2, null) // Show 1st pages


            int pageSubset = ApplicationConfig.ShowTotalPage + 2 < Model.TotalPageCount ? ApplicationConfig.ShowTotalPage + 2 : Model.TotalPageCount - 1;
           @BuildLinks(pageSubset, pageSubset, "...") // Show page subset (...)
 
           @BuildLinks(Model.TotalPageCount, Model.TotalPageCount, null) // Show last page

            return; // Exit

        }
        if (pagesAfter > 4)
        {
            @BuildLinks(1, 1, null) // Show 1st pages

                int pageSubset1 = Model.CurrentPage - 7 > 1 ? Model.CurrentPage - 7 : 2;
                int pageSubset2 = Model.CurrentPage + 7 < Model.TotalPageCount ? Model.CurrentPage + 7 : Model.TotalPageCount - 1;
 
            @BuildLinks(pageSubset1, pageSubset1, pageSubset1 == Model.CurrentPage - 4 ? null : "...") // Show 1st page subset (...)
 
            @BuildLinks(Model.CurrentPage - 3, Model.CurrentPage + 3, null) // Show middle pages

            // Show 2nd page subset (...)
            // only show ... if page is contigous to the previous one.
            @BuildLinks(pageSubset2, pageSubset2, pageSubset2 == Model.CurrentPage + 4 ? null : "...")
            @BuildLinks(Model.TotalPageCount, Model.TotalPageCount, null) // Show last page
        }    
        

}
<div id="pagetitle">
    <h1>@ScreenName.Users.Search</h1>
</div>
<div class="spacer">
</div>
<div id="pagecontent">
    <div class="spacer">
    </div>
    <form action="@Url.Action(ActionConst.Users.Search)" method="GET">
    <div style="margin-left: 150px; margin-right: 150px;">
        <fieldset>
            <legend>Search</legend>
            <table border="0" width="100%">
                <tr>
                    <td width="70%">
                        <div>
                            <label class="required">
                                Serach Criteria</label></div>
                        <div>
                            <input type="text" id="search" name="search" value="@s" style = "width:80%"/>
                            <input type="submit" id="btnSearch" value="Search" onclick="return Validate()" />
                        </div>
                        <div style="font-size: 10px; color: #0085C3">
                            &nbsp;<strong>Login User Id</strong>,<strong>Employee #</strong>, <strong>Last Name</strong>,<strong>First
                                Name</strong>, <strong>Mobile #</strong>, <strong>Email#</strong>,<strong>for all *</strong>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    </form>
    @if (Model != null && (Model.Result == null || Model.Result.Count == 0))
    {
        <div class="doubleSpacer">
        </div>
        <div style="margin-left: 150px; margin-right: 150px;">
            @Html.Partial("_DisplyMessaage", MessageCode.Generic.NoRecordBysearch)
        </div>
        <div class="clear">
        </div>
    }
    @if (Model != null && Model.Result != null && Model.Result.Count > 0)
    {
        <div class="doubleSpacer">
        </div>
        <div class="tab">
            Serarch Results</div>
        <table class="webgrid">
            @* header *@
            <thead>
                <tr class="webgrid-header">
                    <th>
                        Employee#
                    </th>
                    <th>
                        Full Name
                    </th>
                    <th>
                        User Id
                    </th>
                    <th>
                        Department
                    </th>
                    <th>
                        Designation
                    </th>
                    <th>
                        Consultant
                    </th>
                    <th>
                        Mobile#
                    </th>
                    <th>
                        Email
                    </th>
                </tr>
            </thead>
            @{int i = 1;}
            @foreach (var p in Model.Result)
            {   
                <tr class="@(i++ % 2 == 0 ? "webgrid-alternating-row" : "webgrid-row-style")">
                    <td>@p.Id
                    </td>
                    <td>@Html.ActionLink(this.NvlFullName("", p.FirstName, p.Middlename, p.LastName), ActionConst.Users.ViewEmployee, new { id = p.Id })
                    </td>
                    <td>@p.UserId
                    </td>
                    <td>@this.LookupInfo(LookupEnum.Department, p.DepartmentId)
                    </td>
                    <td>@this.LookupInfo(LookupEnum.Designation, p.DesignationId)
                    </td>
                    <td>@(p.IsConsultant ? "Yes" : "No")
                    </td>
                    <td>@p.Mobile
                    </td>
                    <td>@p.Email
                    </td>
                </tr>
            }
            @if (Model.TotalPageCount >0)
            {
                <tfoot>
                    <tr class="webgrid-footer">
                        <td colspan="8">
                            <div align="right">
                               @BuildPaginations() 
                
                            </div>
                        </td>
                    </tr>
                </tfoot>
            }
        </table>
    }
</div>
<div class="clear">
</div>
<div id="modal_dialog" style="background: none repeat scroll 0 0 #EEEEEE">
    Please enter serach criteria.</div>
<script type="text/javascript">
    function Validate() {
        if ($("#search").val().trim() == "") {
            $("#modal_dialog").dialog('open');
            return false;
        } else {
            return true;
        }

    }
    $(document).ready(function () {
        $("#btnSearch").button();
    });

    $("#modal_dialog").dialog({
        resizable: false,
        height: 140,
        width: 400,
        modal: true,
        autoOpen: false,
        title: 'Serach Citeria',
        buttons: {
            "OK": function () {
                $(this).dialog("close");
                $("#search").focus();
            }

        }
    });
</script>
@*      <form id="frmuseSerach" method="POST" action="@Url.Action(ActionConst.Users.Search, ControllerConst.Users)">
                            <input type="text" id="userSearchField" />
                           <img src="@Url.Content("~/Image/magnifier.png")" alt="Search" onclick="SeachUser()" />
                           <div id="modal_dialog_user_search" style="background: none repeat scroll 0 0 #EEEEEE">Please provide search parameter.</div>
   
                      </form>
                        <script type="text/javascript">

                            function SeachUser() {
                                var data = $('#userSearchField').val().trim();
                                if (data == "") {
                                    $("#modal_dialog_user_search").dialog('open');
                                }
                                else {
                                    // $("#frmuseSerach").submit();
                                    document.forms["frmuseSerach"].submit();
                                }

                            }

                            $(document).ready(function () {

                                $("#modal_dialog_user_search").dialog({
                                    resizable: false,
                                    height: 140,
                                    width: 400,
                                    modal: true,
                                    autoOpen: false,
                                    title: 'Search Parameter',
                                    buttons: {
                                        "OK": function () {
                                            $(this).dialog("close");
                                            $('#userSearchField').focus();

                                        }

                                    }
                                });

                                $('#userSearchField').keydown(function (e) {
                                    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
                                    if (key == 13) {
                                        e.preventDefault();
                                        var data = $('#userSearchField').val().trim();

                                        if (data == "") {
                                            $("#modal_dialog_user_search").dialog('open');
                                        }
                                        else {
                                            // $("#frmuseSerach").submit();
                                            document.forms["frmuseSerach"].submit();
                                        }
                                    }
                                });






                            });

                        </script>           
               *@ @*
                <li class="searchContainer">
                        <div>
                            Search</div>
                        <ul id="search">
                            <p>
                                <strong>Search Parametes</strong><br />
                                <br />
                                Login User Id
                                <br />
                                Employee #<br />
                                Last Name<br />
                                Fisrt Name<br />
                                DOB (dd-mm-yyyy)<br />
                                Mobile #<br />
                                Email<br />
                                <br />
                            </p>
                        </ul>
                    </li>
                    <li class="searchContainer">
                    </li>*@